//
//  ViewController.m
//  EchoClient
//
//  Created by Arthur on 15/6/8.
//  Copyright (c) 2015年 Arthur. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UILabel* statusLB;
@property(nonatomic,strong) UITextField* inputFiled;
@property(nonatomic,strong) UIButton* sendBtn;
@property(atomic,assign) UIInterfaceOrientation orien;

@end

@implementation ViewController
@synthesize statusLB;
@synthesize inputFiled;
@synthesize sendBtn;
@synthesize orien;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    statusLB = [[UILabel alloc] init];
    statusLB.text = @"connected";
    
    inputFiled = [[UITextField alloc] init];
    inputFiled.placeholder = @"type word";
    
    sendBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [sendBtn setTitle:@"send" forState:UIControlStateNormal];
    [sendBtn setTintColor:[UIColor blueColor]];
    [self.view addSubview:statusLB];
    [self.view addSubview:inputFiled];
    [self.view addSubview:sendBtn];
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    inputFiled.translatesAutoresizingMaskIntoConstraints = NO;
    statusLB.translatesAutoresizingMaskIntoConstraints = NO;
    sendBtn.translatesAutoresizingMaskIntoConstraints = NO;
}

- (void)viewDidLayoutSubviews{
    [self.view layoutIfNeeded];
}


- (NSArray*)getLConstraints{
    
    NSArray* statusLbVConstraints = @[[NSLayoutConstraint constraintWithItem:statusLB attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.5 constant:0],[NSLayoutConstraint constraintWithItem:statusLB attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
    
    NSArray* inputFieldConstraints = @[[NSLayoutConstraint constraintWithItem:inputFiled attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:0.5 constant:0],[NSLayoutConstraint constraintWithItem:inputFiled attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
    
    NSArray* sendBtnConstraints = @[[NSLayoutConstraint constraintWithItem:sendBtn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:0.5 constant:0],[NSLayoutConstraint constraintWithItem:sendBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
    
    NSMutableArray* arr = [NSMutableArray arrayWithCapacity:5];
    [arr addObjectsFromArray:statusLbVConstraints];
    [arr addObjectsFromArray:inputFieldConstraints];
    [arr addObjectsFromArray:sendBtnConstraints];
    
    return arr;
}

- (NSArray*)getPConstraints{
    
    NSArray* statusLbVConstraints = @[[NSLayoutConstraint constraintWithItem:statusLB attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:statusLB attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
   
    
    NSArray* inputFieldConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusLB]-[inputFiled]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusLB,inputFiled)];
    
    NSArray* sendBtnConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[inputFiled]-[sendBtn]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(inputFiled,sendBtn)];
    
    NSMutableArray* arr = [NSMutableArray arrayWithCapacity:5];
    [arr addObjectsFromArray:statusLbVConstraints];
    [arr addObjectsFromArray:inputFieldConstraints];
    [arr addObjectsFromArray:sendBtnConstraints];
    
    return arr;
}


- (void)viewWillAppear:(BOOL)animated{

}

- (void)viewDidAppear:(BOOL)animated{

}

- (void)viewWillDisappear:(BOOL)animated{

}

- (void)viewDidDisappear:(BOOL)animated{


}

- (NSUInteger)supportedInterfaceOrientations:(UIInterfaceOrientation*)orien{
    
    return UIInterfaceOrientationMaskAll;
}



- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [UIView animateWithDuration:duration animations:^{
        orien = toInterfaceOrientation;
        [self updateViewConstraints];
        [self.view layoutIfNeeded];
    }];
    
}

- (void)updateViewConstraints{
    NSLog(@"need update layout");
    [super updateViewConstraints];
    for (NSLayoutConstraint* constraint in self.view.constraints) {
        [self.view removeConstraint:constraint];
    }
    
    switch (orien) {
        case UIInterfaceOrientationUnknown:
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            [self.view addConstraints:[self getPConstraints]];
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            [self.view addConstraints:[self getLConstraints]];
            break;
        default:
            NSLog(@"error or");
            break;
    }


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
